package com.danielmiller.mygradebook;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EditClass extends Activity {

	EditText name, weight;
	public int class_index, weightInt;
	public String nameString;
	SQLiteDatabase db;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addnewpercent);

		class_index = getIntent().getIntExtra("CLASS_INDEX", -1);
		name = (EditText)findViewById(R.id.new_section_name_edit_text);
		weight = (EditText)findViewById(R.id.new_weight_edit_text);

		final Button createButton = (Button) findViewById(R.id.add_another_section_button);
		createButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				nameString = name.getText().toString();
				weightInt = Integer.parseInt(weight.getText().toString());
				
				db = openOrCreateDatabase(
						"SEMESTER_DB.db"
						, SQLiteDatabase.CREATE_IF_NECESSARY
						, null
				);
				db.setVersion(1);
				db.setLocale(Locale.getDefault());
				db.setLockingEnabled(true);
				
				String query = "INSERT INTO tbl_category" +
				" Values('" + class_index + "','" + nameString + "','" + weightInt + "');";
				db.execSQL(query);

				db.close();

				//Toast
				Toast toast = Toast.makeText(getApplicationContext(), 
						"Section Saved!", Toast.LENGTH_SHORT);
				toast.show();
				
				name.setText("");
				name.hasFocus();
				weight.setText("");
			}
		});
		
		final Button doneButton = (Button) findViewById(R.id.done_button);
		doneButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(EditClass.this, MyGradeBook.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
			}
		});
	}
}